package com.factory.test.activity;

import android.os.Bundle;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.base.BaseTestActivity;
import com.factory.test.R;
import com.factory.test.databinding.ActivityRelayBinding;
import com.yecontech.sdk.base.gpio.GpioRelay;
import com.yecontech.sdk.base.gpio.YTGpio;
import com.yecontech.sdk.base.gpio.YTGpioStatusListener;

/**
 * 继电器测试
 */
public class RelayActivity extends BaseTestActivity {

    private ActivityRelayBinding binding;
    private GpioRelay gpioRelay;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_relay);
        gpioRelay = sdkService.getGpioRelayManager();
        gpioRelay.init(new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {

            }
        });

        binding.open.setChecked(gpioRelay.getStatus(0) == 1);

        binding.open.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    gpioRelay.on(0);
                }else{
                    gpioRelay.off(0);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        gpioRelay.release();
        handler.removeCallbacksAndMessages(null);
    }
}
